package cn.zjxiliu.lib_login.ui.register;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.zjxiliu.lib_core.utils.DeviceUtils;
import cn.zjxiliu.lib_core.widget.ScrollLayout;
import cn.zjxiliu.lib_login.R;
import cn.zjxiliu.lib_login.ui.base.AbsBaseViewImpl;
import cn.zjxiliu.lib_login.widget.InputVerificationClickListener;
import cn.zjxiliu.lib_login.widget.InputViewCallbackListener;
import cn.zjxiliu.lib_login.widget.PasswordInputView;
import cn.zjxiliu.lib_login.widget.PhoneInputNumView;
import cn.zjxiliu.lib_login.widget.VerificationCodeInputView;

/**
 * Created by jessie on 2018/11/21.
 */

public class RegisterViewImpl extends AbsBaseViewImpl implements IRegisterView, ScrollLayout.OnViewChangeListener {
    private RegisterViewHelper registerViewHelper;

    private ImageView ivBack;

    private LinearLayout llBody;

    private ScrollLayout scrollLayout;

    private LinearLayout llRegisterStep1;

    private PhoneInputNumView inputPhone;

    private Button btnNext1;

    private LinearLayout llRegisterStep2;

    private TextView tvPhoneNo;

    private VerificationCodeInputView inputVerification;

    private Button btnNext2;

    private LinearLayout llRegisterStep3;

    private PasswordInputView inputPwd;

    private Button btnFinish;



    @Override
    public void initView(int layoutId) {
        setContentView(layoutId);
        ivBack = (ImageView) this.findViewById(R.id.ivBack);
        scrollLayout = (ScrollLayout) this.findViewById(R.id.scrollLayout);
        llRegisterStep1 = (LinearLayout) this.findViewById(R.id.llRegisterStep1);
        inputPhone = (PhoneInputNumView) this.findViewById(R.id.inputPhone);
        btnNext1 = (Button) this.findViewById(R.id.btnNext1);
        llRegisterStep2 = (LinearLayout) this.findViewById(R.id.llRegisterStep2);
        tvPhoneNo = (TextView) this.findViewById(R.id.tvPhoneNo);
        inputVerification = (VerificationCodeInputView) this.findViewById(R.id.inputCerification);
        btnNext2 = (Button) this.findViewById(R.id.btnNext2);
        llRegisterStep3 = (LinearLayout) this.findViewById(R.id.llRegisterStep3);
        inputPwd = (PasswordInputView) this.findViewById(R.id.inputPwd);
        btnFinish = (Button) this.findViewById(R.id.btnFinish);
        llBody = (LinearLayout) this.findViewById(R.id.llBody);
        initListener();
    }

    private void initListener() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickWhenTouchBack();
            }
        });

        scrollLayout.setIsScroll(false);
        scrollLayout.setOnViewChangeListener(this);
        scrollLayout.setToScreen(registerViewHelper.getCurrentStep());

        inputPhone.setListener(new InputViewCallbackListener() {
            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                if(hasText){
                    btnNext1.setEnabled(true);
                }else{
                    btnNext1.setEnabled(false);
                }
            }
        });




        btnNext1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkPhoneHasRegisterAndGoToStep2();
            }
        });

        inputVerification.setVerificationBtnClickListener(new InputVerificationClickListener() {
            @Override
            public void onVerificationBtnClick() {
                operatorVerificationCodeClick();
            }

            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                if(hasText){
                    btnNext2.setEnabled(true);
                }else{
                    btnNext2.setEnabled(false);
                }
            }
        });


        btnNext2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkVerificationCodeIsRightAndGoToNext();
            }
        });



        inputPwd.setListener(new InputViewCallbackListener() {
            @Override
            public void onFocusChange(boolean isFocus) {

            }

            @Override
            public void inputViewHasText(boolean hasText) {
                if(hasText){
                    btnFinish.setEnabled(true);
                }else{
                    btnFinish.setEnabled(false);
                }
            }
        });
        
        btnFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRegister();
            }
        });
        listenKeyboardLayout(llBody, btnFinish);
    }

    private void startRegister() {
        registerViewHelper.startRegisterAndLogin();
    }

    private void operatorVerificationCodeClick() {
        registerViewHelper.getVerificationCodeWhenRegister(false);
    }

    private void checkPhoneHasRegisterAndGoToStep2() {
        registerViewHelper.checkPhoneHasRegisterAndGotoStep2();
    }


    private void checkVerificationCodeIsRightAndGoToNext(){
        registerViewHelper.checkVerificationCodeIsRightAndGoToStep3();
    }

    @Override
    public void onBackPressed() {
        clickWhenTouchBack();
    }

    private void clickWhenTouchBack() {
        if(registerViewHelper.getCurrentStep() == RegisterViewHelper.REGISTER_STEP_ONE){
            finish();
        }else if(registerViewHelper.getCurrentStep() == RegisterViewHelper.REGISTER_STEP_TWO || registerViewHelper.getCurrentStep() == RegisterViewHelper.REGISTER_STEP_THREE){
            showDialogToCheckBackToLogin();
        }
    }

    private void showDialogToCheckBackToLogin() {
    }

    @Override
    protected void init() {
        registerViewHelper = new RegisterViewHelper(this);
        registerViewHelper.initHelper();
    }

    @Override
    protected void cancelTask() {

    }

    @Override
    public void onVerificationCodeStart() {
        inputVerification.startCountDownTimer();
    }

    @Override
    public void finishAllViews() {
        this.setResult(this.RESULT_OK);
        finish();
    }

    @Override
    public void onViewChange(int view) {
        DeviceUtils.closeKeybord(this);
        registerViewHelper.setCurrentStep(view);
    }
}
